<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Myclass {

    private $CI;

    public function __construct() {
        $this->CI = & get_instance();
    }

    public function checkuser() {
        if (($this->CI->session->userdata('user'))) {
            $u = $this->CI->session->userdata('user');
            $user = $this->CI->db->select('email, pass')->from('users')->where('email', $u['email'])->get()->row_array();
            if (isset($user) && count($user)) {
                if ($u['email'] == $user['email'] && $u['pass'] == $user['pass'])
                    return TRUE;
            }
            return FALSE;
        }
        return FALSE;
    }

    public function checkcustomer() {
        if (($this->CI->session->userdata('customer'))) {
            $u = $this->CI->session->userdata('customer');
            $user = $this->CI->db->select('email, pass')->from('customers')->where('email', $u['email'])->get()->row_array();
            if (isset($user) && count($user)) {
                if ($u['email'] == $user['email'] && $u['pass'] == $user['pass'])
                    return TRUE;
            }
            return FALSE;
        }
        elseif (isset($_COOKIE['customerinfo']) && !empty($_COOKIE['customerinfo'])) {
            $cookie = $_COOKIE['customerinfo'];
            $cookie = json_decode($cookie, TRUE);
            $user = $this->CI->db->where('email', $cookie['email'])->get('customers')->row_array();
            if (isset($user) && count($user)) {
                if ($cookie['email'] == $user['email'] && $cookie['pass'] == $user['pass'])
                    $this->CI->session->set_userdata('customer',$user);
                    return TRUE;
            }
            return FALSE;
        } else
            return FALSE;
    }

    public function sendmail($para = array()) {
        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'quangdv02@gmail.com',
            'smtp_pass' => 'congnghethongtin',
            'mailtype' => 'html',
            'charset' => 'utf-8'
        );
        $this->CI->load->library('email', $config);
        $this->CI->email->set_newline("\r\n");
        $this->CI->email->from('quangdv02@gmail.com', $para['name']);
        $this->CI->email->to($para['to']);
        $this->CI->email->subject($para['subject']);
        $this->CI->email->message($para['message']);

        $this->CI->email->send();
    }
    
    public function js_redirect($alert, $url) {
        die('<meta charset="UTF-8"><script type="text/javascript">alert(\'' . $alert . '\');location.href = \'' . $url . '\';</script>');
    }

}
